package com.payment.ktb.activity.main4;

import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.constants.Constants;
import com.payment.ktb.constants.ConstantsSettle;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.Md5Utils;
import com.payment.ktb.utils.SharedPreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by syd on 2016/6/14.
 */
public class SettleQueryActivity extends BaseActivity {



    @BindView(R.id.tv_settlequery_qr_settleamount)
    TextView tv_settlequery_qr_settleamount;
    @BindView(R.id.tv_settlequery_qr_totalamount)
    TextView tv_settlequery_qr_totalamount;
    @BindView(R.id.tv_settlequery_qrfast_settleamount)
    TextView tv_settlequery_qrfast_settleamount;
    @BindView(R.id.tv_settlequery_qrfast_totalamount)
    TextView tv_settlequery_qrfast_totalamount;
    @BindView(R.id.iv_settlequery_eyes)
    ImageView iv_settlequery_eyes;
    @BindView(R.id.tv_settlequery_totalamount)
    TextView tv_settlequery_totalamount;
    @BindView(R.id.tv_settlequery_normal_settleamount)
    TextView tv_settlequery_normal_settleamount;
    @BindView(R.id.tv_settlequery_normal_totalamount)
    TextView tv_settlequery_normal_totalamount;
    @BindView(R.id.tv_settlequery_fast_settleamount)
    TextView tv_settlequery_fast_settleamount;
    @BindView(R.id.tv_settlequery_fast_totalamount)
    TextView tv_settlequery_fast_totalamount;





    boolean show = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_settlequery);
        ButterKnife.bind(this);
        initTitle("钱包");
    }


    @Override
    protected void onResume() {
        super.onResume();
        settleQuery();
    }

    @OnClick({R.id.rl_settlequery_eyes,R.id.btn_settlequery_fast_settle,R.id.btn_settlequery_normal_settle,R.id.btn_settlequery_qr_settle,R.id.btn_settlequery_qrfast_settle})
    public void clickEvent(View view) {

        switch (view.getId()) {


            //隐藏金额
            case R.id.rl_settlequery_eyes:
                if (show) {
                    tv_settlequery_totalamount.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    iv_settlequery_eyes.setImageResource(R.mipmap.eyes_wallet_show);
                    show = false;
                } else {
                    tv_settlequery_totalamount.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    iv_settlequery_eyes.setImageResource(R.mipmap.eyes_wallet_hide);
                    show = true;
                }
                break;



            //快捷
            case R.id.btn_settlequery_fast_settle:
                ConstantsSettle.payType = ConstantsSettle.payTypeNocard;
                ConstantsSettle.orderType = ConstantsSettle.orderTypeT0;
                startActivity(SettleSubmitActivity.class);

                break;



            //普通
            case R.id.btn_settlequery_normal_settle:
                ConstantsSettle.payType = ConstantsSettle.payTypeNocard;
                ConstantsSettle.orderType = ConstantsSettle.orderTypeT1;
                startActivity(SettleSubmitActivity.class);
                break;



            //微信普通
            case R.id.btn_settlequery_qr_settle:
                ConstantsSettle.payType = ConstantsSettle.payTypeQr;
                ConstantsSettle.orderType = ConstantsSettle.orderTypeT1;
                startActivity(SettleSubmitActivity.class);
                break;



            //微信快捷
            case R.id.btn_settlequery_qrfast_settle:
                ConstantsSettle.payType = ConstantsSettle.payTypeQr;
                ConstantsSettle.orderType = ConstantsSettle.orderTypeT0;
                startActivity(SettleSubmitActivity.class);
                break;


        }


    }





    /**
     *
     */
    private void settleQuery() {


        progressDialogUtils.showProgress();

        //构造传输参数
       String token = SharedPreferencesUtils.getString(ConstantsUser.userToken);
        //MD5（orderType+key）
        String sign = Md5Utils.md5Hex(SharedPreferencesUtils.getString(ConstantsUser.userKey));


        //将参数放入map中
        HashMap<String, String> map = new HashMap<String, String>();

        map.put("token", token);
        map.put("sign", sign);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.nocardSettleBalanceState, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                progressDialogUtils.dismissProgress();

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String code = jsonObject.optString("code");
                    if (Constants.NORMAL_CODE.equals(code)) {

                        ConstantsSettle.allBalance = jsonObject.optString("allBalance");
                        ConstantsSettle.t0AllBalanceNocard = jsonObject.optString("t0AllBalanceNocard");
                        ConstantsSettle.t0EnBalanceNocard = jsonObject.optString("t0EnBalanceNocard");
                        ConstantsSettle.t1AllBalanceNocard = jsonObject.optString("t1AllBalanceNocard");
                        ConstantsSettle.t1EnBalanceNocard = jsonObject.optString("t1EnBalanceNocard");
                        ConstantsSettle.t1AllBalanceQr = jsonObject.optString("t1AllBalanceQr");
                        ConstantsSettle.t1EnBalanceQr = jsonObject.optString("t1EnBalanceQr");
                        ConstantsSettle.t0AllBalanceQr = jsonObject.optString("t0AllBalanceQr");
                        ConstantsSettle.t0EnBalanceQr = jsonObject.optString("t0EnBalanceQr");

                        tv_settlequery_totalamount.setText(ConstantsSettle.allBalance);
                        tv_settlequery_fast_totalamount.setText(ConstantsSettle.t0AllBalanceNocard);
                        tv_settlequery_normal_totalamount.setText(ConstantsSettle.t1AllBalanceNocard);
                        tv_settlequery_fast_settleamount.setText(ConstantsSettle.t0EnBalanceNocard);
                        tv_settlequery_normal_settleamount.setText(ConstantsSettle.t1EnBalanceNocard);
                        tv_settlequery_qr_totalamount.setText(ConstantsSettle.t1AllBalanceQr);
                        tv_settlequery_qr_settleamount.setText(ConstantsSettle.t1EnBalanceQr);
                        tv_settlequery_qrfast_totalamount.setText(ConstantsSettle.t0AllBalanceQr);
                        tv_settlequery_qrfast_settleamount.setText(ConstantsSettle.t0EnBalanceQr);

                    } else {

                        AlertDialogUtils.showAlertDialog(mContext, jsonObject.optString("msg"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {

                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.fail_to_connect_server));
            }
        });
    }






}
